<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

use think\Db;

// 发送短信验证码
function send_mobile_code($mobile)
{
    if (preg_match("/^1[0-9]{1}\d{9}$/", $mobile)) {
        require_once 'extra\curl.func.php';
        $code = rand(100000, 999999);
        $appkey = 'f0a76d04ea803c58';
        $content = '欢迎您申请加入湖南网络工程职业学院计算机协会，您本次的验证码为：' . $code . '，请保存好不要随意给其他人，有效期为10分钟。【网院计算机协会】';//utf8
        $url = "http://api.jisuapi.com/sms/send?appkey=$appkey&mobile=$mobile&content=$content";
        $result = curlOpen($url);
        $jsonarr = json_decode($result, true);
        if ($jsonarr['status'] != 0) {
            return [-1, $jsonarr['msg']];
        }
//        $data = [
//            'mobile' => $mobile,
//            'msg' => $content,
//            'code' => $code,
//            'status' => 0,
//            'send_time' => date("Y-m-d H:i:s", time()),
//        ];
//        Db::name('msg_logs')->insert($data);
        return [1, '发送成功'];
    }else{
        return [0, '手机号码格式错误'];
    }
}
function send_mobile_mianshi(){
    $results = Db::name('test')->select();
    foreach($results as $test){
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "http://apis.haoservice.com/sms/sendv2?mobile=".$test['mobile']."&tpl_id=4117&content=【网院计算机协会】".$test['name']."同学：感谢你报名加入湖南网络工程职业学院计算机协会，请于2018年9月27日中午12：10赶到教学楼二楼241参加面试，祝你面试成功！&key=5e286952aa61457e81bab7aa13139fd2",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            echo $response;
        }

    }
}


function test(){
    header('content-type:text/html;charset=utf-8');

    $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL

    $smsConf = array(
        'key'   => '59741d5e53161a3493b8269a8a5da9d7', //您申请的APPKEY
        'mobile'    => '1891351****', //接受短信的用户手机号码
        'tpl_id'    => '111', //您申请的短信模板ID，根据实际情况修改
        'tpl_value' =>'#code#=1234&#company#=聚合数据' //您设置的模板变量，根据实际情况修改
    );

    $content = juhecurl($sendUrl,$smsConf,1); //请求发送短信

    if($content){
        $result = json_decode($content,true);
        $error_code = $result['error_code'];
        if($error_code == 0){
            //状态为0，说明短信发送成功
            echo "短信发送成功,短信ID：".$result['result']['sid'];
        }else{
            //状态非0，说明失败
            $msg = $result['reason'];
            echo "短信发送失败(".$error_code.")：".$msg;
        }
    }else{
        //返回内容异常，以下可根据业务逻辑自行修改
        echo "请求发送短信失败";
    }


}

function send_mobile_code_old($mobile)
{
    if (preg_match("/^1[0-9]{1}\d{9}$/", $mobile)) {

        $data = include 'application/api/config-' . WANCLL_ENV . '.php';
        $bami = $data['sms']['bami'];

        $smsname = $bami['account'];
        $smspwd = $bami['password'];
        $smscompany = $bami['company'];

        $code = rand(100000, 999999);

        $content = "【" . $smscompany . "】您的验证码是：" . $code . "。请不要把验证码泄露给其他人。如非本人操作，可不用理会！";

        $result = file_get_contents('http://wancllsms.wancll.net/api/sms/Send.ashx?action=Send&uid=' . $smsname . '&passwd=' . $smspwd . '&content=' . $content . '&phonelist=' . $mobile);

        $result = json_decode($result, true);
        if ($result['msg1'] != 'success') {
            $data = [
                'mobile' => $mobile,
                'msg' => $content,
                'code' => $code,
                'status' => 0,
                'send_time' => date("Y-m-d H:i:s", time()),
            ];
            Db::name('mobile_msgs')->insert($data);

            return [0, '发送失败'];
        } else {
            $data = [
                'mobile' => $mobile,
                'msg' => $content,
                'code' => $code,
                'status' => 1,
                'send_time' => date("Y-m-d H:i:s", time()),
            ];
            Db::name('mobile_msgs')->insert($data);

            return [1, '发送成功'];
        }

    } else {
        return [0, '手机号码格式错误'];
    }
}

/**
 * 请求接口返回内容
 * @param  string $url [请求的URL地址]
 * @param  string $params [请求的参数]
 * @param  int $ipost [是否采用POST形式]
 * @return  string
 */
function juhecurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}

// 校验短信验证码
function check_mobile_code($mobile, $code)
{
    // $result = Db::name('mobile_msgs')->where(['mobile' => $mobile])->order('id DESC')->limit('1')->select();
    // if ($result) {
    //     //验证码10分钟内有效
    //     if ($result[0]['code'] == $code && (time() - strtotime($result[0]['send_time'])) <= 600) {
    //         return true;
    //     } else {
    //         return false;
    //     }
    // } else {
    //     return false;
    // }

    return true;
}

function wancll_md5($param)
{
    return md5(md5($param . '@wancll'));
}

/**
 * 返回带协议的域名
 */
function get_host()
{
    $host = $_SERVER["HTTP_HOST"];
    $protocol = is_ssl() ? "https://" : "http://";
    return $protocol . $host;
}

/**
 * 判断是否SSL协议
 * @return boolean
 */
function is_ssl()
{
    if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
        return true;
    } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
        return true;
    }
    return false;
}

/**
 * 处理base64位图片集
 * @return boolean
 */
function base64_to_img($arr, $new_width = '', $new_height = '', $max_size = '')
{
    $result = [];

    foreach ($arr as $key => $value) {
        if (strpos($value, '/public') === false) {
            $base_img = str_replace('data:image/jpeg;base64,', '', $value);
            $base_img = str_replace('data:image/jpg;base64,', '', $base_img);
            $base_img = str_replace('data:image/png;base64,', '', $base_img);
            $base_img = str_replace('data:image/gif;base64,', '', $base_img);

            $dir = './public/upload/images/';
            $filename = time() . rand(100, 999) . '.jpg';
            $output_file = $dir . $filename;
            file_put_contents($output_file, base64_decode($base_img));

            // 处理图片宽高
            if ($new_width && $new_height) {
                deal_img($output_file, $new_width, $new_height);
            }

            // 处理图片大小
            if ($max_size) {
                $size = filesize($output_file);
                $rate = 100;
                while ($size > $max_size) {
                    deal_img($output_file, '', '', $rate);
                    $size = filesize($output_file);
                    $rate -= 10;

                    if ($rate == 0) {
                        break;
                    }
                }
            }

            $result[] = '/public/upload/images/' . $filename;

        } else {
            $output_file = '.' . $value;
            // 处理图片宽高
            if ($new_width && $new_height) {
                deal_img($output_file, $new_width, $new_height, 100);
            }

            // 处理图片大小
            if ($max_size) {
                $size = filesize($output_file);
                $rate = 100;
                while ($size > $max_size) {
                    deal_img($output_file, '', '', $rate);
                    $size = filesize($output_file);
                    $rate -= 10;

                    if ($rate == 0) {
                        break;
                    }
                }
            }

            $result[] = $value;
        }
    }

    return $result;
}

/**
 * desription 压缩图片
 * @param sting $imgsrc 图片路径
 * @param string $imgdst 压缩后保存路径
 * @param number $rate 压缩比率 0（最差质量，文件最小）到 100（最佳质量，文件最大）
 */
function deal_img($imgsrc, $new_width = '', $new_height = '', $rate = 100, $imgdst = '')
{
    list($width, $height, $type) = getimagesize($imgsrc);

    if (!$new_width) {
        $new_width = $width;
    }

    if (!$new_height) {
        $new_height = $height;
    }

    if (!$imgdst) {
        $imgdst = $imgsrc;
    }

    switch ($type) {
        case 1:
            $giftype = check_gifcartoon($imgsrc);
            if ($giftype) {
                header('Content-Type:image/gif');
                $image_wp = imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefromgif($imgsrc);
                imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                imagejpeg($image_wp, $imgdst, $rate); // 图像质量，范围从 0（最差质量，文件最小）到 100（最佳质量，文件最大），默认75
                imagedestroy($image_wp);
            }
            break;
        case 2:
            header('Content-Type:image/jpeg');
            $image_wp = imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefromjpeg($imgsrc);
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            imagejpeg($image_wp, $imgdst, $rate);
            imagedestroy($image_wp);
            break;
        case 3:
            header('Content-Type:image/png');
            $image_wp = imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefrompng($imgsrc);
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            imagejpeg($image_wp, $imgdst, $rate);
            imagedestroy($image_wp);
            break;
    }
}

/**
 * desription 判断是否gif动画
 * @param sting $image_file图片路径
 * @return boolean t 是 f 否
 */
function check_gifcartoon($image_file)
{
    $fp = fopen($image_file, 'rb');
    $image_head = fread($fp, 1024);
    fclose($fp);
    return preg_match("/" . chr(0x21) . chr(0xff) . chr(0x0b) . 'NETSCAPE2.0' . "/", $image_head) ? false : true;
}

/**
 * 把返回的数据集转换成Tree
 */
function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
{
    // 创建Tree
    $tree = array();
    if (is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] = &$list[$key];
        }
        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] = &$list[$key];
            } else {
                if (isset($refer[$parentId])) {
                    $parent = &$refer[$parentId];
                    $parent[$child][] = &$list[$key];
                }
            }
        }
    }

    return $tree;
}

/**
 * 模拟提交数据，获得返回值
 */
function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

/**
 * 生成二维码
 * @author YiYun
 */
function qrcode($data, $path = '')
{
    vendor('phpqrcode.phpqrcode');

    $filename = rand(100, 999) . time();
    if (empty($data)) {
        return '参数错误';
    }
    if (empty($path)) {
        $path = './public/upload/qrcodes/' . $filename . '.png';
    }
    QRcode::png($data, $path, 'L', 4);

    $path = '/public/upload/qrcodes/' . $filename . '.png';

    return $path;
}

function checklogin(){

}
